package com.fang.shiyan;

public class Depository {
    //首先，定义产品的初始值为0；
    private int product = 0;


    //建立生产车间
    public synchronized void produce(){

            if (product == 1) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("仓库已经没有产品，进行生产。。。。。。。。。");
                product++;
                System.out.println("仓库已经生产出商品。。。。。" + product);
                this.notifyAll();
            }

    }

    //建立消费窗口
    public synchronized void consume() {

            if (product == 0) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("消费者进行购买中。。。。。。。。。");
                Consumer c = (Consumer) Thread.currentThread();
                c.setProducts(c.getProducts() + 1);
                product--;
                System.out.println("消费者获得的产品数目为："+c.getProducts());
                System.out.println("消费者购买成功，产品剩余：" + product);

            }
        }



}
